package org.thirdHero.os.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.thirdHero.core.constant.CommonConstant;
import org.thirdHero.core.util.ImageUtil;
import org.thirdHero.core.util.SystemUtil;
import org.thirdHero.os.config.OsConfig;
import org.thirdHero.os.domain.OsProperties;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Stream;
@Service
public class FileSystemStorageService implements StorageService {
    private Logger log = LoggerFactory.getLogger(getClass());
    private OsProperties osProperties;
    private static Path rootLocation ;


    public void init(){
        try {
            if(SystemUtil.getSystem().equals(SystemUtil.Mac_OS_X)){
                rootLocation = Paths.get(osProperties.getMac());
            }else if(SystemUtil.getSystem().contains(SystemUtil.WINDOWS)){
                rootLocation = Paths.get(osProperties.getWindows());
            }else{
                rootLocation = Paths.get(osProperties.getLinux());
            }
            Files.createDirectories(rootLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void store(InputStream inputStream, String filename) {
        try {
            Files.copy(inputStream, this.rootLocation.resolve(filename), StandardCopyOption.REPLACE_EXISTING);
        }
        catch (IOException e) {
            throw new RuntimeException ("Failed to store file " + filename, e);
        }
    }


    @Override
    public Stream<Path> loadAll() {
        try {
            return Files.walk(this.rootLocation, 1)
                    .filter(path -> !path.equals(this.rootLocation))
                    .map(path -> this.rootLocation.relativize(path));
        }
        catch (IOException e) {
            throw new RuntimeException ("Failed to read stored files", e);
        }

    }

    @Override
    public Path load(String filename) {
        return rootLocation.resolve(filename);
    }

    @Override
    public Resource loadAsResource(String filename) {
        try {
            Path file = load(filename);
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            }
            else {
                Path File404 = load("404.jpg");
                Resource resource404 = new UrlResource(File404.toUri());
                return resource404;
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void delete(String filename) {
        Path file = load(filename);
        try {
            Files.delete(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存缩略图
     * @param name
     */
    @Override
    public void storeThumbnail( String name) {
        String path = rootLocation.resolve(name).toAbsolutePath().toString();
        ImageUtil imageUtil = new ImageUtil();
        imageUtil.thumbnailImage(path,320,200, CommonConstant.THUMNAIL_URL_SUFFIX,true);
    }

    public OsProperties getOsProperties() {
        return osProperties;
    }

    public void setOsProperties(OsProperties osProperties) {
        this.osProperties = osProperties;
        init();
    }
}